

import pymysql

db = pymysql.connect(
    host='localhost',
    user='root',
    password='',
    database='company',
    port=3306,
    charset='utf8'
)
cursor=db.cursor()
# sql="create table person_info(username varchar(20),age int,sex char(4),height double(3,2))"
# cursor.execute(sql)
# db.commit()
#
# cursor.close()
# db.close()
# print(data,type(data))
# for i in data:
#     print(i)
#
#
n=1
def c_user():
    try:
        username=input("请输入你的名字：")
        age=int(input("请输入你的年龄："))
        sex=input("请输入你的性别：")
        height=float(input("请输入你的身高："))
        user_info={"user_name":username,"user_age":age,"user_sex":sex,"user_height":height}
        return user_info
    except Exception as e:
        print("输入格式错误，请重新输入！",e)
while n in range(1,11):
    try:
        n+=1
        new_user=c_user()
        print("输入信息成功！")
        sql = "insert into person_info (username,age,sex,height) values(%s,%s,%s,%s)"
        cursor.execute(sql,(new_user["user_name"],new_user["user_age"],new_user["user_sex"],new_user["user_height"]))
        db.commit()
        print("已存入数据库！可以输入下一个人的信息")
    except Exception as f:
        print("输入格式错误，请重新输入！",f)
else:
    print("已存入10人")


